package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.CureMethodDAO;
import cn.edu.scau.cmi.hombio.dao.CurePigDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskCourseDAO;
import cn.edu.scau.cmi.hombio.dao.CureTaskMethodDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.CureMethod;
import cn.edu.scau.cmi.hombio.domain.CurePig;
import cn.edu.scau.cmi.hombio.domain.CureTaskCourse;
import cn.edu.scau.cmi.hombio.domain.CureTaskMethod;
import cn.edu.scau.cmi.hombio.domain.Task;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for CureTaskCourse entities
 * 
 */

@Service("CureTaskCourseService")

@Transactional
public class CureTaskCourseServiceImpl implements CureTaskCourseService {

	/**
	 * DAO injected by Spring that manages CureMethod entities
	 * 
	 */
	@Autowired
	private CureMethodDAO cureMethodDAO;

	/**
	 * DAO injected by Spring that manages CurePig entities
	 * 
	 */
	@Autowired
	private CurePigDAO curePigDAO;

	/**
	 * DAO injected by Spring that manages CureTaskCourse entities
	 * 
	 */
	@Autowired
	private CureTaskCourseDAO cureTaskCourseDAO;

	/**
	 * DAO injected by Spring that manages CureTaskMethod entities
	 * 
	 */
	@Autowired
	private CureTaskMethodDAO cureTaskMethodDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Instantiates a new CureTaskCourseServiceImpl.
	 *
	 */
	public CureTaskCourseServiceImpl() {
	}

	/**
	 * Return all CureTaskCourse entity
	 * 
	 */
	@Transactional
	public List<CureTaskCourse> findAllCureTaskCourses(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<CureTaskCourse>(cureTaskCourseDAO.findAllCureTaskCourses(startResult, maxRows));
	}

	/**
	 * Save an existing CureTaskMethod entity
	 * 
	 */
	@Transactional
	public CureTaskCourse saveCureTaskCourseCureTaskMethods(Integer id, CureTaskMethod related_curetaskmethods) {
		CureTaskCourse curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(id, -1, -1);
		CureTaskMethod existingcureTaskMethods = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(related_curetaskmethods.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureTaskMethods != null) {
			existingcureTaskMethods.setId(related_curetaskmethods.getId());
			existingcureTaskMethods.setMemo(related_curetaskmethods.getMemo());
			related_curetaskmethods = existingcureTaskMethods;
		}

		related_curetaskmethods.setCureTaskCourse(curetaskcourse);
		curetaskcourse.getCureTaskMethods().add(related_curetaskmethods);
		related_curetaskmethods = cureTaskMethodDAO.store(related_curetaskmethods);
		cureTaskMethodDAO.flush();

		curetaskcourse = cureTaskCourseDAO.store(curetaskcourse);
		cureTaskCourseDAO.flush();

		return curetaskcourse;
	}

	/**
	 * Delete an existing CureMethod entity
	 * 
	 */
	@Transactional
	public CureTaskCourse deleteCureTaskCourseCureMethods(Integer curetaskcourse_id, Integer related_curemethods_id) {
		CureMethod related_curemethods = cureMethodDAO.findCureMethodByPrimaryKey(related_curemethods_id, -1, -1);

		CureTaskCourse curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(curetaskcourse_id, -1, -1);

		related_curemethods.setCureTaskCourse(null);
		curetaskcourse.getCureMethods().remove(related_curemethods);

		cureMethodDAO.remove(related_curemethods);
		cureMethodDAO.flush();

		return curetaskcourse;
	}

	/**
	 * Delete an existing CureTaskMethod entity
	 * 
	 */
	@Transactional
	public CureTaskCourse deleteCureTaskCourseCureTaskMethods(Integer curetaskcourse_id, Integer related_curetaskmethods_id) {
		CureTaskMethod related_curetaskmethods = cureTaskMethodDAO.findCureTaskMethodByPrimaryKey(related_curetaskmethods_id, -1, -1);

		CureTaskCourse curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(curetaskcourse_id, -1, -1);

		related_curetaskmethods.setCureTaskCourse(null);
		curetaskcourse.getCureTaskMethods().remove(related_curetaskmethods);

		cureTaskMethodDAO.remove(related_curetaskmethods);
		cureTaskMethodDAO.flush();

		return curetaskcourse;
	}

	/**
	 * Return a count of all CureTaskCourse entity
	 * 
	 */
	@Transactional
	public Integer countCureTaskCourses() {
		return ((Long) cureTaskCourseDAO.createQuerySingleResult("select count(o) from CureTaskCourse o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public void deleteCureTaskCourse(CureTaskCourse curetaskcourse) {
		cureTaskCourseDAO.remove(curetaskcourse);
		cureTaskCourseDAO.flush();
	}

	/**
	 * Save an existing CureMethod entity
	 * 
	 */
	@Transactional
	public CureTaskCourse saveCureTaskCourseCureMethods(Integer id, CureMethod related_curemethods) {
		CureTaskCourse curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(id, -1, -1);
		CureMethod existingcureMethods = cureMethodDAO.findCureMethodByPrimaryKey(related_curemethods.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcureMethods != null) {
			existingcureMethods.setId(related_curemethods.getId());
			existingcureMethods.setMemo(related_curemethods.getMemo());
			related_curemethods = existingcureMethods;
		}

		related_curemethods.setCureTaskCourse(curetaskcourse);
		curetaskcourse.getCureMethods().add(related_curemethods);
		related_curemethods = cureMethodDAO.store(related_curemethods);
		cureMethodDAO.flush();

		curetaskcourse = cureTaskCourseDAO.store(curetaskcourse);
		cureTaskCourseDAO.flush();

		return curetaskcourse;
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public CureTaskCourse saveCureTaskCourseTask(Integer id, Task related_task) {
		CureTaskCourse curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		curetaskcourse.setTask(related_task);
		related_task.getCureTaskCourses().add(curetaskcourse);
		curetaskcourse = cureTaskCourseDAO.store(curetaskcourse);
		cureTaskCourseDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return curetaskcourse;
	}

	/**
	 * Load an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public Set<CureTaskCourse> loadCureTaskCourses() {
		return cureTaskCourseDAO.findAllCureTaskCourses();
	}

	/**
	 * Save an existing CurePig entity
	 * 
	 */
	@Transactional
	public CureTaskCourse saveCureTaskCourseCurePigs(Integer id, CurePig related_curepigs) {
		CureTaskCourse curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(id, -1, -1);
		CurePig existingcurePigs = curePigDAO.findCurePigByPrimaryKey(related_curepigs.getId());

		// copy into the existing record to preserve existing relationships
		if (existingcurePigs != null) {
			existingcurePigs.setId(related_curepigs.getId());
			existingcurePigs.setCureTime(related_curepigs.getCureTime());
			existingcurePigs.setMemo(related_curepigs.getMemo());
			related_curepigs = existingcurePigs;
		}

		related_curepigs.setCureTaskCourse(curetaskcourse);
		curetaskcourse.getCurePigs().add(related_curepigs);
		related_curepigs = curePigDAO.store(related_curepigs);
		curePigDAO.flush();

		curetaskcourse = cureTaskCourseDAO.store(curetaskcourse);
		cureTaskCourseDAO.flush();

		return curetaskcourse;
	}

	/**
	 * Save an existing CureTaskCourse entity
	 * 
	 */
	@Transactional
	public void saveCureTaskCourse(CureTaskCourse curetaskcourse) {
		CureTaskCourse existingCureTaskCourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(curetaskcourse.getId());

		if (existingCureTaskCourse != null) {
			if (existingCureTaskCourse != curetaskcourse) {
				existingCureTaskCourse.setId(curetaskcourse.getId());
				existingCureTaskCourse.setStep(curetaskcourse.getStep());
				existingCureTaskCourse.setTime(curetaskcourse.getTime());
				existingCureTaskCourse.setMemo(curetaskcourse.getMemo());
			}
			curetaskcourse = cureTaskCourseDAO.store(existingCureTaskCourse);
		} else {
			curetaskcourse = cureTaskCourseDAO.store(curetaskcourse);
		}
		cureTaskCourseDAO.flush();
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public CureTaskCourse deleteCureTaskCourseTask(Integer curetaskcourse_id, Integer related_task_id) {
		CureTaskCourse curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(curetaskcourse_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		curetaskcourse.setTask(null);
		related_task.getCureTaskCourses().remove(curetaskcourse);
		curetaskcourse = cureTaskCourseDAO.store(curetaskcourse);
		cureTaskCourseDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return curetaskcourse;
	}

	/**
	 */
	@Transactional
	public CureTaskCourse findCureTaskCourseByPrimaryKey(Integer id) {
		return cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(id);
	}

	/**
	 * Delete an existing CurePig entity
	 * 
	 */
	@Transactional
	public CureTaskCourse deleteCureTaskCourseCurePigs(Integer curetaskcourse_id, Integer related_curepigs_id) {
		CurePig related_curepigs = curePigDAO.findCurePigByPrimaryKey(related_curepigs_id, -1, -1);

		CureTaskCourse curetaskcourse = cureTaskCourseDAO.findCureTaskCourseByPrimaryKey(curetaskcourse_id, -1, -1);

		related_curepigs.setCureTaskCourse(null);
		curetaskcourse.getCurePigs().remove(related_curepigs);

		curePigDAO.remove(related_curepigs);
		curePigDAO.flush();

		return curetaskcourse;
	}
}
